// 点击登录按钮，弹出模态对话框

function Auth() {
  var self = this;
  self.maskWrapper = $('.mask-wrapper');
  self.signinBtn = $('.signin-btn');
  self.signupBtn = $('.signup-btn');
  self.closeBtn = $('.close-btn');
  self.scrollWrapper = $('.scroll-wrapper');
  self.switcher = $('.switch');
}
Auth.prototype.run = function() {
  var self = this;
  self.listenShowHideEvent();
  self.listenSigninEvent();
};
Auth.prototype.showEvent = function() {
  var self = this;
  self.maskWrapper.show();
};
Auth.prototype.hideEvent = function() {
  var self = this;
  self.maskWrapper.hide();
};
Auth.prototype.listenShowHideEvent = function() {
  var self = this;
  self.signinBtn.click(function() {
    self.showEvent();
    self.scrollWrapper.css({ left: 0 });
  });
  self.signupBtn.click(function() {
    self.showEvent();
    self.scrollWrapper.css({ left: -400 });
  });
  self.closeBtn.click(function() {
    self.hideEvent();
  });
  self.maskWrapper.click(function(e) {
    // console.log('maskclick', e);
    if (e.target === e.currentTarget) self.hideEvent();
  });

  self.switcher.click(function() {
    var currentLeft = self.scrollWrapper.css('left');
    currentLeft = parseInt(currentLeft);
    if (currentLeft < 0) {
      self.scrollWrapper.animate({ left: '0' });
    } else {
      self.scrollWrapper.animate({ left: '-400px' });
    }
  });
};
Auth.prototype.listenSigninEvent = function() {
  var self = this;
  var signingroup = $('.signin-group');
  var telephoneInput = signingroup.find('input[name=telephone]');
  var passwordInput = signingroup.find('input[name=password]');
  var rememberInput = signingroup.find('input[name=remember]');
  var submitbtn = signingroup.find('.submit-btn');
  submitbtn.click(function() {
    var telephone = telephoneInput.val();
    var password = passwordInput.val();
    var remember = rememberInput.prop('checked') ? 1 : 0;
    xfzajax.post({
      url: '/account/login/',
      data: { telephone: telephone, password: password, remember: remember },
      success: function(result) {
        console.log(result);
        if (result.code === 200) {
          self.hideEvent();
          window.location.reload();
        } else {
          var message = result['message'];
          if (typeof message === 'string' || message.constructor === String) {
            alert(message);
          } else {
            for (var key in message) {
              var messages = message[0];
              console.log(messages[0]);
            }
          }
        }
      },
      error: function(result) {
        console.log(result);
      },
      fail: function(result) {
        console.log(result);
      }
    });
  });
};
$(function() {
  var auth = new Auth();
  auth.run();
});
